package com.cssw.fyzb.tps.api.pay;

import com.cssw.fyzb.tps.service.pay.AliPayService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author: chenchao
 * @Date: 2020/7/14 22:29
 */


@Function(description = "支付宝app下单", parameters = {
        @Parameter(name = "body", type = Type.String, description = "内容-实名认证费用", required = true),
        @Parameter(name = "subject", type = Type.String, description = "主题-智链视界", required = true),
        @Parameter(name = "outTradeNo", type = Type.String, description = "外部单号", required = true),
        @Parameter(name = "amount", type = Type.String, description = "金额", required = true)
})
@Component
public class AliAppPay implements Action {

    private static final Logger logger = LoggerFactory.getLogger(AliAppPay.class);

    @Autowired
    private AliPayService aliPayService;

    @Override
    public Response execute(Request request) throws Exception {
        String body = request.getString("body");
        String subject = request.getString("subject");
        String outTradeNo = request.getString("outTradeNo");
        String amount = request.getString("amount");

        try {
            Map map = aliPayService.appPay(outTradeNo, body, subject, amount);
            return Response.success().put("data", map);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("支付发起失败");
        }
    }
}
